package com.util.timerTask;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;

import com.Entity.RecNewsListInfo;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.util.textProcess.Calculate_Vector_Similarity;
import com.util.textProcess.ConnectionPool;

@Component
public class UpdateRecNews {

	// 定义推荐的信息数
	private static int recommend_num = 30;

	static ConnectionPool pool = null;
	static Connection conn;
	static PreparedStatement stmt;
	static Statement sta;

	static {

		pool = ConnectionPool.getInstance();
		conn = (Connection) pool.getConnection();
	}

	// get userlist
	public static Set<Integer> getUserIdSet() throws SQLException {
		Set<Integer> userIdSet = new HashSet<Integer>();
		String sql = "SELECT userId FROM usersInfo";
		sta = (Statement) conn.createStatement();
		ResultSet rs = sta.executeQuery(sql);
		int userid = 0;
		while (rs.next()) {
			userid = rs.getInt("userid");
			userIdSet.add(userid);
		}
		return userIdSet;
	}

	// get recnewslist
	public static List<Integer> getRecNewsIdList(Integer userid)
			throws SQLException {
		List<Integer> recInfoIdList = new ArrayList<Integer>();
		String sql = "SELECT recInfoId FROM recNewsListInfo WHERE userId=?";
		stmt = (PreparedStatement) conn.prepareStatement(sql);
		stmt.setLong(1, userid);
		ResultSet rs = stmt.executeQuery();
		int recInfoId = 0;
		while (rs.next()) {
			recInfoId = rs.getInt("recInfoId");
			recInfoIdList.add(recInfoId);
		}
		return recInfoIdList;
	}

	// save recnewslist
	public static void saveRecNews(RecNewsListInfo recNewsInfo)
			throws SQLException {
		String sql = "insert into recNewsListInfo(userId,newsId,updateTime) values (?,?,now())";
		stmt = (PreparedStatement) conn.prepareStatement(sql);
		stmt.setInt(1, recNewsInfo.getUserId());
		stmt.setLong(2, recNewsInfo.getNewsId());
		stmt.executeUpdate();
	}

	// update recnewslist
	public static void updateRecNews(RecNewsListInfo recNewsInfo)
			throws SQLException {
		String sql = "update recNewsListInfo set userId=?,newsId=?,updateTime=now() where recInfoId=?";
		stmt = (PreparedStatement) conn.prepareStatement(sql);
		stmt.setInt(1, recNewsInfo.getUserId());
		stmt.setLong(2, recNewsInfo.getNewsId());
		stmt.setLong(3, recNewsInfo.getRecInfoId());
		stmt.executeUpdate();
	}

	public void excuteUpdateRecNews() throws SQLException, IOException {
		Set<Integer> userIdSet = getUserIdSet();
		for (int i : userIdSet) {
			getOrUpdateRecNews(i);
		}

	}

	public void getOrUpdateRecNews(int userId) throws SQLException, IOException {
		RecNewsListInfo recNewsInfo;
		// 查询当前用户是否已有推荐列表
		List<Integer> recInfoList = getRecNewsIdList(userId);
		ArrayList<Long> recommend_fileIdList = Calculate_Vector_Similarity
				.getRecommendFileIDList(userId);
		// 没有推荐列表就产生并保存推荐列表
		if (recInfoList.isEmpty()) {
			for (int i = 0; i < recommend_num; i++) {
				recNewsInfo = new RecNewsListInfo();
				recNewsInfo.setNewsId(recommend_fileIdList.get(i));
				recNewsInfo.setUserId(userId);
				recNewsInfo.setUpdateTime(new Date());
				saveRecNews(recNewsInfo);
			}
		} else {
			// 如果存在就更新推荐列表
			for (int i = 0; i < recommend_num; i++) {
				recNewsInfo = new RecNewsListInfo();
				recNewsInfo.setRecInfoId(recInfoList.get(i));
				recNewsInfo.setNewsId(recommend_fileIdList.get(i));
				recNewsInfo.setUserId(userId);
				recNewsInfo.setUpdateTime(new Date());
				updateRecNews(recNewsInfo);
			}
		}
	}
}
